
function doLogin(){

    var name = $('#username').val();
    var pass = $('#password').val();
    console.log(name,pass);

    if(name && pass){
        API.api_post('web_login',{login_type:'normal',mobile:name,password:pass},dataBack);
    }


}

var dataBack = function(msg,getStatus){

    if(getStatus){
        switch (msg.user_type){
            case 'normal':
                messageBox("您是普通用户,没有权限登陆本系统,请开设认证药店后再次登陆",'alert');
                break;
            case 'director':
                messageBox("欢迎店长<strong>" + msg.nickname +"</strong>登陆本系统",'msg');
                setToken(msg.objectId,1);
                setTimeout(function(){
                    if(msg.pharmacy_count == 1){
                        window.location.href = 'index.html?pharmacy='+msg.director_pharmacy;
                    }else{
                        window.location.href = 'pharmacy.html';
                    }

                },2000);
                console.log(msg);
                break;
            case 'manager':
                messageBox("欢迎经理<strong>" + msg.nickname +"</strong>登陆本系统",'msg');
                setToken(msg.objectId,1);
                setTimeout(function(){
                    window.location.href = 'pharmacy.html';
                },2000);
                break;
            default :
                messageBox("您是普通用户,没有权限登陆本系统,请开设认证药店后再次登陆",'alert');
                console.log(msg);
                break;
        }
    }else{
        //发生错误
        messageBox(msg,'alert');
        return false;
    }

};

function setToken(token,type) {
    //避免cookie读取不到的问题，添加localstorge
    if(token){
        //先写cookie
        date = new Date();
        timestamp = date.getTime();
        if(parseInt(type) == 1){
            $.cookie('user_token',token,{expires:7,path:'/'});
            localStorage.setItem('user_token',token);
            //给locatstorage设置过期时间
            expired_time = new Date(timestamp + 7 * 24 * 3600 * 1000);
            localStorage.setItem('user_token_'+token,expired_time);
            cookie_set = $.cookie('user_token');
            local_set = localStorage.getItem('user_token');
            return cookie_set || local_set
        }else{
            $.cookie('user_token',token,{path:'/'});
            sessionStorage.user_token = token;
            local_set = sessionStorage.user_token;
            cookie_set = $.cookie('user_token');
            return cookie_set || local_set
        }

    }else{
        return false;
    }
    // body...
}